package com.easyspringboot.bulider;

import com.easyspringboot.bean.Constants;
import com.easyspringboot.bean.FieldInfo;
import com.easyspringboot.bean.TableInfo;
import com.easyspringboot.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildMapper
 * @Time 3:38
 */
public class BuildService {
    private static final Logger logger = LoggerFactory.getLogger(BuildService.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_SERVICE);
        String ClassName = tableInfo.getBeanName() + Constants.SUFFIX_BEAN_SERVICE;
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File poFile = new File(folder, ClassName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);
            bw.write("package " + Constants.PACKAGE_SERVICE + ";");
            bw.newLine();
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_PO + "." + tableInfo.getBeanName() + ";");
            bw.newLine();
            bw.newLine();
            bw.write("import java.util.List;");
            bw.newLine();
            bw.newLine();
            bw.write("import "+Constants.PACKAGE_QUERY+"."+tableInfo.getBeanParamName()+";");
            bw.newLine();
            bw.newLine();
            bw.write("import "+Constants.PACKAGE_VO+".PaginationResultVO;");
            bw.newLine();
            bw.newLine();

            //创建类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "业务接口");
            bw.write("public interface " + ClassName +  "{");
            bw.newLine();
            createServiceComment(bw, "根据条件查询列表");

            bw.write("\tList<" + tableInfo.getBeanName() + "> selectByParam(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            createServiceComment(bw, "根据条件查询数量");
            bw.newLine();
            bw.write("\tInteger selectCountByParam(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            createServiceComment(bw, "分页查询");
            bw.newLine();
            bw.write("\tPaginationResultVO<" + tableInfo.getBeanName() + "> findListByPage(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            createServiceComment(bw, "新增");
            bw.newLine();
            bw.write("\tInteger add(" + tableInfo.getBeanName() + " bean);");
            bw.newLine();
            createServiceComment(bw, "批量新增");
            bw.newLine();
            bw.write("\tInteger addBatch(List<" + tableInfo.getBeanName() + "> listBean);");
            bw.newLine();

            createServiceComment(bw, "批量新增/修改");
            bw.newLine();
            bw.write("\tInteger addOrUpdateBatch(List<" + tableInfo.getBeanName() + "> listBean);");



            for (Map.Entry<String, List<FieldInfo>> entry :tableInfo.getKeyIndexMap().entrySet()) {
                String methodNameStr = null;
                List<FieldInfo> keyFieldInfos = entry.getValue();//主键字段信息
                Integer index = 0;
                Integer index_annotation = 0;
                StringBuffer methodName = new StringBuffer();//方法名
                StringBuffer annotationName = new StringBuffer();//注解名
                StringBuffer methodParams = new StringBuffer();//方法名
                for (FieldInfo fieldInfo : keyFieldInfos) {
                    methodName.append(StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName()));
                    annotationName.append(fieldInfo.getComment());
                    if (index<methodParams.length()-1) {
                        methodParams.append(",");
                    }
                    if (index_annotation < keyFieldInfos.size() - 1) {
                        annotationName.append(",");
                    }
                    if (index < keyFieldInfos.size()-1) {
                        methodName.append("And");
                        methodNameStr = methodName.substring(0, methodName.length() - 3);
                    }else {
                        methodNameStr = methodName.toString();
                    }
                    methodParams.append(fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName());

                }
                bw.newLine();
                createServiceComment(bw, "根据" + annotationName + "查询");
                bw.newLine();
                bw.newLine();
                bw.write("\t "+tableInfo.getBeanName()+" getBy" + methodNameStr + "("+methodParams+");");
                bw.newLine();
                bw.newLine();
                createServiceComment(bw, "根据" + annotationName + "更新");
                bw.newLine();
                bw.newLine();
                bw.write("\t Integer updateBy" + methodNameStr + "("+tableInfo.getBeanName()+" bean , "+methodParams+ ");");
                bw.newLine();
                bw.newLine();
                createServiceComment(bw, "根据" + annotationName + "删除");
                bw.newLine();
                bw.newLine();
                bw.write("\t Integer deleteBy" + methodNameStr + "("+methodParams+");");
                bw.newLine();
                bw.newLine();
            }


            bw.write("}");
            bw.newLine();


        } catch (Exception e) {
            logger.error("创建Service失败", e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    //注释
    public static void createServiceComment(BufferedWriter bw, String comment) throws IOException {
        bw.write("\t/**");
        bw.newLine();
        bw.write("\t * "+comment);
        bw.newLine();
        bw.write("\t**/");
        bw.newLine();
    }

}

